package com.ourcraft.informationcollectionsystem.service.province.investigationManage;

import com.ourcraft.informationcollectionsystem.entity.InvestigationPeriodPattern;
import com.ourcraft.informationcollectionsystem.mapper.InvestigationPeriodPatternMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 调查期管理模块调查期模式相关的服务实现
 *
 * @author Z0136
 * @since 2023/11/4
 */
@Service
public class InvestigationPatternServiceImpl implements InvestigationPatternService {
    @Autowired
    private InvestigationPeriodPatternMapper investigationPatternMapper;
    @Autowired
    InvestigationCreatorImpl investigationCreator;


    /**
     * 删除调查期模式
     *
     * @param patternId 删除的模式Id
     * @return 是否删除成功
     */
    @Override
    public Boolean deletePattern(int patternId) {
        return investigationPatternMapper.deleteById(patternId) > 0;
    }

    /**
     * 创建调查期模式
     *
     * @param pattern 创建的调查期模式属性
     * @return 是否创建成功
     */
    @Override
    public Boolean createPattern(InvestigationPeriodPattern pattern) {
        boolean success = investigationPatternMapper.insert(pattern) > 0;
        if (!success) return false;
        // 创建时判断一次是否需要根据模式创建调查期
        investigationCreator.createInvestigationByPattern(pattern);
        return true;
    }

    /**
     * 修改调查期模式
     *
     * @param pattern 修改后调查期模式属性
     * @return 是否修改成功
     */
    @Override
    public Boolean modifyPattern(InvestigationPeriodPattern pattern) {
        return investigationPatternMapper.updateById(pattern) > 0;
    }

    /**
     * 查询所有调查期模式信息
     *
     * @return 所有调查期模式信息
     */
    @Override
    public List<InvestigationPeriodPattern> getAllPatterns() {
        return investigationPatternMapper.selectList(null);
    }
}
